﻿<?php
require_once("db_functions.php");
require_once("page.class.php");	

define("PAGE_MAX",15);

class cenovnik extends page{
	var $db;
	
	function cenovnik($get){
		$this->db = new db_functions();
		$this->loadConfig();
		$action = getFromInput("a");
		
		switch ($action){
			case "newCena":
				$this->newCena();
				break;
			case "editCena":
				$this->editCena($get["id"],$get["p"]);
				break;
			case "delCena":
				$this->delCena($get["id"]);
				break;
			case "saveCena":
				$this->saveCena($get);
				break;
			case "cenovnici":
				$mod = getFromInput("mod");
				$this->assign("message_display","none");
				switch ($mod){
					case "new":
						$this->newCenovnik();
						$this->browseCenovnici();
						break;
					case "delete":
						$this->delCenovnik();
						$this->browseCenovnici();
						break;
					default:
						$this->browseCenovnici();
						break;
				}
				break;
			default:
				$this->browse();
				break;
		}
	}
	
	function browse(){
		//--------- FILTER
		$page = getFromInput("p");
		if ($page<1) {
			$page = 1;
		}
		$fArtikl = getFromInput("filter_artikl");
		$fTip = getFromInput("filter_tip");
		$fBoja = getFromInput("filter_boja");
		$fMera = getFromInput("filter_mera");
		$fJm = getFromInput("filter_jm");
		$fCenovnik = getFromInput("filter_cenovnik");
		$fCena = getFromInput("filter_cena");
		
		$cenovnici = $this->get_Cenovnici($fCenovnik);
			$this->assign("cenovnik_id",$cenovnici["polje_id"]);
			$this->assign("cenovnik_start",$cenovnici["polje_start"]);
			$this->assign("cenovnik_start_string",$cenovnici["polje_start_string"]);
			$this->assign("cenovnik_selected",$cenovnici["polje_selected"]);
		if($fCenovnik < 1){
			$cenovnik = $cenovnici["polje_id"][0];
			$fCenovnik=$cenovnik;
		}else{
			$cenovnik=$fCenovnik;
		}
		
		$filter_string = "&filter_artikl=$fArtikl";
		$filter_string .= "&filter_tip=$fTip";
		$filter_string .= "&filter_boja=$fBoja";
		$filter_string .= "&filter_mera=$fMera";
		$filter_string .= "&filter_jm=$fJm";
		$filter_string .= "&filter_cenovnik=$fCenovnik";
		$filter_string .= "&filter_cena=$fCena";
		//echo $filter_string;
		$this->assign("filter_string",$filter_string);
		
		
		
		$polja = $this->get_Artikli($fArtikl);
			list($polja["polje_id"],$polja["polje_ime"],$polja["polje_selected"]) = removeZeroVals($polja["polje_id"],$polja["polje_ime"],$polja["polje_selected"]);
			$this->assign("art_id",$polja["polje_id"]);
			$this->assign("art_name",$polja["polje_ime"]);
			$this->assign("filter_artikl_selected",$polja["polje_selected"]);
		$polja = $this->get_ArtikliTipovi($fTip);
			list($polja["polje_id"],$polja["polje_ime"],$polja["polje_selected"]) = removeZeroVals($polja["polje_id"],$polja["polje_ime"],$polja["polje_selected"]);
			$this->assign("art_tip_id",$polja["polje_id"]);
			$this->assign("art_tip_name",$polja["polje_ime"]);
			$this->assign("filter_arttip_selected",$polja["polje_selected"]);
		$polja = $this->get_ArtikliBoje($fBoja);
			list($polja["polje_id"],$polja["polje_code"],$polja["polje_selected"]) = removeZeroVals($polja["polje_id"],$polja["polje_code"],$polja["polje_selected"]);
			$this->assign("art_boja_id",$polja["polje_id"]);
			$this->assign("art_boja_name",$polja["polje_code"]);
			$this->assign("filter_artboja_selected",$polja["polje_selected"]);
		$this->assign("filter_mera",$fMera);
		$polja = $this->get_Mere($fJm);
			list($polja["polje_id"],$polja["polje_code"],$polja["polje_selected"]) = removeZeroVals($polja["polje_id"],$polja["polje_code"],$polja["polje_selected"]);
			$this->assign("jm_id",$polja["polje_id"]);
			$this->assign("jm_name",$polja["polje_code"]);
			$this->assign("jm_selected",$polja["polje_selected"]);
		$polja = $this->get_Kampanje($filter["f_kampanja"]);
			list($polja["polje_id"],$polja["polje_ime"],$polja["polje_selected"]) = removeZeroVals($polja["polje_id"],$polja["polje_ime"],$polja["polje_selected"]);
			$this->assign("kampanja_id",$polja["polje_id"]);
			$this->assign("kampanja_name",$polja["polje_ime"]);
			$this->assign("filter_kampanja_selected",$polja["polje_selected"]);
		$this->assign("f_cena",$fCena);
		
			
		
		
		$q = "SELECT * FROM cene WHERE cena_cenovnik=$cenovnik";
		
		if($fArtikl > 0){
			$q .= " AND cena_art_id=".$fArtikl;
		}
		if($fTip > 0){
			$q .= " AND cena_art_tip=".$fTip;
		}
		if($fBoja > 0){
			$q .= " AND cena_art_boja=".$fBoja;
		}
		if($fMera > 0){
			$q .= " AND cena_art_mera=$fMera";
			if($fJm>0){
				$q .= " AND cena_art_jed_mere=$fJm";
			}
		}
		if($fCena > 0){
			$qCena = str_replace(".","",$fCena);
			$qCena = str_replace(",",".",$qCena);
			$q .= " AND ROUND(cena_value,2)='".$qCena."'";
		}
		$q .= " ORDER BY cena_value DESC";
		
		$res_all = $this->db->select1($q);
		if($res_all[0]){
			$ukupno = count($res_all);
			$this->assign("info","Ukupno: $ukupno");
			$num_pages = bcdiv($ukupno,PAGE_MAX,0);
			if($ukupno%PAGE_MAX){
				$num_pages++;
			}
			if($num_pages>1){
				for ($i=1; $i<=$num_pages; $i++){
					$pages[] = $i;
					if($i == $page){
						$selected_page[] = " id=\"current\"";
					}else{
						$selected_page[] = "";
					}
				}
				$this->assign("selected_page",$selected_page);
				$this->assign("page",$pages);
			}else{
				$this->assign("start_comment_page","<!--");
				$this->assign("end_comment_page","-->");
			}
		}else{
			$this->assign("start_comment_page","<!--");
			$this->assign("end_comment_page","-->");
		}
		
		$start = ($page-1)*PAGE_MAX;
			
		$q .= " LIMIT $start,".PAGE_MAX;
		$res = $this->db->select2($q);
		if(!$res[0]){
			$this->assign("error_message","<h3 style=\"clear:both\">Nema definisanih cena za odabrani cenovnik.</h3>");
			$this->assign("start_comment","<!--");
			$this->assign("end_comment","-->");
		}else{
			foreach ($res as $row){
				$cena_ids[] = $row["cena_id"];
				$cena_art_ids[] = $row["cena_art_id"];
				$cena_artikl[] = $this->getArtklText($row["cena_art_id"],$row["cena_art_tip"],$row["cena_art_boja"],$row["cena_art_mera"],$row["cena_art_jed_mere"]);
				$cena_kamps[] = $this->db->getOne("SELECT kampanja_naziv FROM kampanje WHERE kampanja_id=".$row["cena_kamp_id"]);
				$cena_vals[] = currency($row["cena_value"]);
				$cena_cenovnik[] = $row["cena_cenovnik"];
				
				$porez = $row["cena_porez"];
				if($porez){
					$r = $this->db->getRow("SELECT porez_name, porez_percent FROM porezi WHERE porez_id=$porez LIMIT 1");
					$porez = $r[0]." (".$r[1]."%)";
				}else{
					$porez = "";
				}
				
				$cena_porez[] = $porez;
			}
			$this->assign("cena_id",$cena_ids);
			$this->assign("cena_art_id",$cena_ids);
			$this->assign("cena_artikl",$cena_artikl);
			$this->assign("cena_kampanja",$cena_kamps);
			$this->assign("cena_val",$cena_vals);
			$this->assign("cena_cenovnik",$cena_cenovnik);
			$this->assign("porez",$cena_porez);
		}
		$this->assign("curr_page",$page);
		echo $this->parseTemplate("cenovnik/browse.tpl");
	}
	function newCena(){
		
		$polja = $this->get_Artikli();
			$this->assign("art_id",$polja["polje_id"]);
			$this->assign("art_name",$polja["polje_ime"]);
		$polja = $this->get_ArtikliTipovi();
			$this->assign("art_tip_id",$polja["polje_id"]);
			$this->assign("art_tip_name",$polja["polje_ime"]);
		$polja = $this->get_ArtikliBoje();
			$this->assign("art_boja_id",$polja["polje_id"]);
			$this->assign("art_boja_name",$polja["polje_ime"]);
			$this->assign("art_boja_code",$polja["polje_code"]);
		$polja = $this->get_Mere();
			$this->assign("art_jm_id",$polja["polje_id"]);
			$this->assign("art_jm_name",$polja["polje_ime"]);
			$this->assign("art_jm_code",$polja["polje_code"]);
		$polja = $this->get_Porezi();
			$this->assign("porez_id",$polja["polje_id"]);
			$this->assign("porez_name",$polja["polje_ime"]);
			$this->assign("porez_perc",$polja["polje_perc"]);
		$polja = $this->get_Kampanje();
			$this->assign("kampanja_id",$polja["polje_id"]);
			$this->assign("kampanja_name",$polja["polje_ime"]);
		$polja = $this->get_Cenovnici();
			$this->assign("cenovnik_id",$polja["polje_id"]);
			$this->assign("cenovnik_start",$polja["polje_start"]);
			$this->assign("cenovnik_start_text",$polja["polje_start_string"]);
		
		$this->assign("cena_id","-1");
		$this->assign("title","Nova cena");
		$this->assign("cenovnik",'0 ');
		$this->assign("page",'0 ');
		
		echo $this->parseTemplate("cenovnik/edit.tpl");
	}
	function editCena($id,$page){
		$res = $this->db->select2("SELECT * FROM cene WHERE cena_id=$id LIMIT 1");
		$res = $res[0];
		
		$polja = $this->get_Artikli($res["cena_art_id"]);
			$this->assign("art_id",$polja["polje_id"]);
			$this->assign("art_name",$polja["polje_ime"]);
			$this->assign("selected_artikl",$polja["polje_selected"]);
			
		$polja = $this->get_ArtikliTipovi($res["cena_art_tip"]);
			$this->assign("art_tip_id",$polja["polje_id"]);
			$this->assign("art_tip_name",$polja["polje_ime"]);
			$this->assign("selected_tip",$polja["polje_selected"]);
		$polja = $this->get_ArtikliBoje($res["cena_art_boja"]);
			$this->assign("art_boja_id",$polja["polje_id"]);
			$this->assign("art_boja_name",$polja["polje_ime"]);
			$this->assign("art_boja_code",$polja["polje_code"]);
			$this->assign("selected_boja",$polja["polje_selected"]);
		$this->assign("art_mera",$res["cena_art_mera"]);
		$polja = $this->get_Mere($res["cena_art_jed_mere"]);
			$this->assign("art_jm_id",$polja["polje_id"]);
			$this->assign("art_jm_name",$polja["polje_ime"]);
			$this->assign("art_jm_code",$polja["polje_code"]);
			$this->assign("selected_jm",$polja["polje_selected"]);
		if($res["cena_value"]){
			$this->assign("cena",number_format($res["cena_value"],2,",","."));
		}
		$polja = $this->get_Cenovnici($res["cena_cenovnik"]);
			$this->assign("cenovnik_id",$polja["polje_id"]);
			$this->assign("cenovnik_start",$polja["polje_start"]);
			$this->assign("cenovnik_start_text",$polja["polje_start_string"]);
			$this->assign("selected_cenovnik",$polja["polje_selected"]);
		
		$this->assign("cena_id",$id);
		$this->assign("cenovnik",$res["cena_cenovnik"]);
		$this->assign("page",$page);
		$this->assign("title","Izmena cene");
		echo $this->parseTemplate("cenovnik/edit.tpl");
	}
	function saveCena($get){
		
		$get["cena"] = str_replace(".","",$get["cena"]);
		$get["cena"] = str_replace(",",".",$get["cena"]);
		
		if($get["id"] < 0){
			$q = "INSERT INTO cene VALUES('',"
				."'".$get["artid"]."',"
				."'".$get["arttip"]."',"
				."'".$get["artboja"]."',"
				."'".$get["artmera"]."',"
				."'".$get["artjm"]."',"
				."'".$get["cena"]."',"
				."'".$get["cenovnik"]."')";
			$msg = "Cena je dodata.";
		}else{		
			$q = "UPDATE cene SET "
				."cena_art_id='".$get["artid"]."', "
				."cena_art_tip='".$get["arttip"]."', "
				."cena_art_boja='".$get["artboja"]."', "
				."cena_art_jed_mere='".$get["artjm"]."', "
				."cena_art_mera='".$get["artmera"]."', "
				."cena_value='".$get["cena"]."', "
				."cena_cenovnik='".$get["cenovnik"]."' "
				."WHERE cena_id=".$get["id"];
			$msg = "Cena je sačuvana";
		}
		if($this->db->alterTable($q)){
			echo $msg;
		}else{
			echo mysql_error();
		}
	}
	function delCena($id){
		if($this->db->alterTable("DELETE FROM cene WHERE cena_id=$id")){
			echo "Cena je izbrisana.";
		}else{
			echo mysql_error();
		}
	}
	
	//---------------------------------
	//		CENOVNICI
	//---------------------------------
	function browseCenovnici(){
		
		$polja = $this->get_Cenovnici();
			$this->assign("id",$polja["polje_id"]);
			$this->assign("date",$polja["polje_start_string"]);
		
		echo $this->parseTemplate("cenovnik/cenovnici.tpl");
	}
	function newCenovnik(){
		
		$date = getFromInput("new_cenovnik_date");
		if (preg_match('/^\d{1,2}\.\d{1,2}\.\d{2,4}$/',$date)) {
			$time = date2int($date);
			$q = "INSERT INTO cenovnici(cenovnik_start) VALUES($time)";
			if ($this->db->alterTable($q)) {
				$this->message("Cenovnik je sačuvan");
			}else{
				$this->message(mysql_error());
			}
		}else{
			$this->message("Datum početka važenja cenovnika nije validan");
		}
	}
	function delCenovnik(){
		$id = getFromInput("id");
		$date = getFromInput("date");
		
		$q = "DELETE FROM cenovnici WHERE cenovnik_id=$id LIMIT 1";
		if ($this->db->alterTable($q)) {
			$this->message("Cenovnik #$id ($date) je obrisan.");
		}else{
			$this->message(mysql_error());
		}
	}
	
	function getArtklText($id,$tip,$boja,$mera,$jed_mere){
		$art = $this->db->getOne("SELECT art_name FROM artikli WHERE art_id=$id LIMIT 1");
		$tip = $this->db->getOne("SELECT arttip_name FROM artikli_tipovi WHERE arttip_id=$tip LIMIT 1");
		$col = $this->db->getOne("SELECT art_boja_code FROM artikli_boje WHERE art_boja_id=$boja LIMIT 1");
		$jm = $this->db->getOne("SELECT unit_svalue FROM jedinice_mere WHERE unit_id=$jed_mere LIMIT 1");
		
		$ret = $art;
		if($tip)$ret.= " / $tip";
		if($col)$ret.= " / $col";
		if($mera>0){
			$ret.= " / $mera ".strtolower($jm).".";
		}
		
		return $ret;
	}
	
	//------------------------------------------------
	//			GET FUNCTIONS
	//------------------------------------------------
	function get_Cenovnici($id=0){
		$res = $this->db->select2("SELECT * FROM cenovnici ORDER BY cenovnik_start DESC");
		$ret = array();
		foreach ($res as $k=>$v){
			$ret["polje_id"][$k] = $v["cenovnik_id"];
			$ret["polje_start"][$k] = $v["cenovnik_start"];
			$ret["polje_start_string"][$k] = date($this->getFromConfig("short_date"),$v["cenovnik_start"]);
			if($ret["polje_id"][$k] == $id){
				$ret["polje_selected"][$k] = " selected";
			}else{
				$ret["polje_selected"][$k] = "";
			}
		}
		return $ret;
	}
	function get_Artikli($id=0){
		$res = $this->db->select2("SELECT art_id, art_name FROM artikli ORDER BY art_name");
		$ret = array();
		foreach ($res as $k=>$v){
			$ret["polje_id"][$k] = $v["art_id"];
			$ret["polje_ime"][$k] = $v["art_name"];
			if($ret["polje_id"][$k] == $id && $id){
				$ret["polje_selected"][$k] = " selected";
			}else{
				$ret["polje_selected"][$k] = "";
			}
		}
		return $ret;
	}
	function get_ArtikliTipovi($id=0){
		$res = $this->db->select2("SELECT * FROM artikli_tipovi ORDER BY arttip_name");
		$ret = array();
		foreach ($res as $k=>$v){
			$ret["polje_id"][$k] = $v["arttip_id"];
			$ret["polje_ime"][$k] = $v["arttip_name"];
			if($ret["polje_id"][$k] == $id && $id){
				$ret["polje_selected"][$k] = " selected";
			}else{
				$ret["polje_selected"][$k] = "";
			}
		}
		return $ret;
	}
	function get_ArtikliBoje($id=0){
		$res = $this->db->select2("SELECT * FROM artikli_boje ORDER BY art_boja_code");
		$ret = array();
		foreach ($res as $k=>$v){
			$ret["polje_id"][$k] = $v["art_boja_id"];
			$ret["polje_ime"][$k] = $v["art_boja_name"];
			$ret["polje_code"][$k] = $v["art_boja_code"];
			if($ret["polje_id"][$k] == $id && $id){
				$ret["polje_selected"][$k] = " selected";
			}else{
				$ret["polje_selected"][$k] = "";
			}
		}
		return $ret;
	}
	function get_Mere($id=0){
		$res = $this->db->select2("SELECT * FROM jedinice_mere ORDER BY unit_svalue");
		$ret = array();
		foreach ($res as $k=>$v){
			$ret["polje_id"][$k] = $v["unit_id"];
			$ret["polje_ime"][$k] = $v["unit_value"];
			$ret["polje_code"][$k] = $v["unit_svalue"];
			if($ret["polje_id"][$k] == $id && $id){
				$ret["polje_selected"][$k] = " selected";
			}else{
				$ret["polje_selected"][$k] = "";
			}
		}
		return $ret;
	}
	function get_Porezi($id=0){
		$res = $this->db->select2("SELECT * FROM porezi ORDER BY porez_name");
		$ret = array();
		foreach ($res as $k=>$v){
			$ret["polje_id"][$k] = $v["porez_id"];
			$ret["polje_ime"][$k] = $v["porez_name"];
			$ret["polje_perc"][$k] = $v["porez_percent"];
			if($ret["polje_id"][$k] == $id && $id){
				$ret["polje_selected"][$k] = " selected";
			}else{
				$ret["polje_selected"][$k] = "";
			}
		}
		return $ret;
	}
	function get_ArtikliPaketi($cenovnik,$id=null){
		$ret = array();
		
		$cenovnik_start = $this->db->getOne("SELECT cenovnik_start FROM cenovnici WHERE cenovnik_id=$cenovnik LIMIT 1");
		$q = "SELECT ap.*, a.art_name, at.arttip_name, ab.art_boja_code, c.cena_art_mera, jm.unit_svalue, apa.popust "
			."FROM artikli a, artikli_paketi ap, artikli_paketi_artikli apa, cene c, artikli_tipovi at, artikli_boje ab, jedinice_mere jm "
			."WHERE ap.paketid=apa.paket "
			."AND apa.cena=c.cena_id "
			."AND c.cena_paket=1 "
			."AND c.cena_art_id=a.art_id "
			."AND c.cena_art_tip=at.arttip_id "
			."AND c.cena_art_boja=ab.art_boja_id "
			."AND c.cena_art_jed_mere=jm.unit_id "
			."AND $cenovnik_start<=ap.paket_end ";
		$res = $this->db->select2($q);
		if ($res !== false) {
			foreach ($res as $row){
				$pid = $row["paketid"];
				$data[$pid]["name"] = $row["paketname"];
				$data[$pid]["cena"] = $row["cena"];
				$data[$pid]["start"] = date($this->getFromConfig("short_date"),$row["paket_start"]);
				$data[$pid]["start_time"] = $row["paket_start"];
				$data[$pid]["end"] = date($this->getFromConfig("short_date"),$row["paket_end"]);
				$data[$pid]["end_time"] = $row["paket_end"];
				$data[$pid]["opis"] = $row["paketname"];
				
				if (isset($data[$pid]["sadrzaj"])) {
					$data[$pid]["sadrzaj"] .= "<br />".$row["art_name"];
				}else{
					$data[$pid]["sadrzaj"] = $row["art_name"];
				}
				
				if (strlen($row["arttip_name"])) {
					$data[$pid]["sadrzaj"] .= "-".$row["arttip_name"];
				}
				if (strlen($row["art_boja_code"])) {
					$data[$pid]["sadrzaj"] .= "-".$row["art_boja_code"];
				}
				if ($row["cena_art_mera"]>0) {
					$data[$pid]["sadrzaj"] .= "-".$row["cena_art_mera"].strtolower($row["unit_svalue"]).".";
				}
				$data[$pid]["sadrzaj"] .= ", popust: ".$row["popust"]."%";
				
			}
			$i=0;
			foreach ($data as $k=>$v){
				$ret["polje_id"][$i]		= $k;
				$ret["polje_ime"][$i]		= $v["name"];
				$ret["polje_cena"][$i]		= $v["cena"];
				$ret["polje_start"][$i]		= $v["start"];
				$ret["polje_start_time"][$i]= $v["start_time"];
				$ret["polje_end"][$i]		= $v["end"];
				$ret["polje_end_time"][$i]	= $v["end_time"];
				$ret["polje_opis"][$i]		= $v["opis"];
				$ret["polje_sadrzaj"][$i]	= $v["sadrzaj"];
				if ($k == $id) {
					$ret["polje_selected"][$i] = " checked";
				}else{
					$ret["polje_selected"][$i] = "";
				}
				
				$i++;
			}
		}
		return $ret;
	}
	
	//----------------------------------------------
	function message($txt){
		$this->assign("message_display","block");
		$this->assign("message",$txt);
	}
}
?>